<template>
  <el-dialog v-dialogDrag :title="dialogTitle" :close-on-click-modal="false" class="dialogStyleSmall" :visible.sync="dialogVisible">
    <el-form v-if="dialogVisible" ref="addForm" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="form.name" size="medium" placeholder />
      </el-form-item>
      <el-form-item label="类型顺序" prop="sort">
        <el-input-number
          v-model="form.sort"
          size="medium"
          controls-position="right"
          :min="1"
        />
      </el-form-item>
      <el-form-item label="备注信息" prop="remark">
        <el-input v-model="form.remark" type="textarea" size="medium" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">

      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button size="medium" :disabled="btnDisabled" type="primary" @click="submitForm">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  updateCompanyTempType,
  addCompanyTempType,
  selectMaxSequenceCode
} from '@/api/enterprise/company-temp-type'
export default {
  name: 'AddDialog',
  components: {},
  data() {
    return {
      // 弹出的对话框的标题
      dialogTitle: '',
      // 对话框是否弹出显示
      dialogVisible: false,
      form: {
        id: '',
        name: '',
        sort: 1,
        // status: '0',
        remark: ''
      },
      btnDisabled: false,
      // from表单校验
      rules: {
        name: [
          { required: true, message: '类型名称不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '必填', trigger: 'blur' }
        ]
      },
      maxData: 0

    }
  },
  methods: {
    handleAdd(title, row) {
      this.dialogTitle = title
      this.dialogVisible = true
      if (row) {
        const { id, name, sort, remark } = row
        this.$nextTick(() => {
          this.form = { name, sort, remark, id }
        })
      } else {
        this.getMaxNum()
      }
    },
    // 获取最大排序
    getMaxNum() {
      selectMaxSequenceCode().then(resp => {
        if (resp) {
          this.maxData = resp.data
          this.form.sort = this.maxData
        } else {
          this.msgError(resp.message)
        }
      })
    },
    emptyForm() {
      this.form.id = ''
      this.resetForm('addForm')
    },
    // 弹框取消按钮
    cancel() {
      this.dialogVisible = false
      this.emptyForm()
      this.btnDisabled = false
    },
    // 弹框确认按钮
    submitForm() {
      this.$refs['addForm'].validate(valid => {
        if (valid) {
          this.btnDisabled = true
          if (this.form.id) { // 更新
            updateCompanyTempType(this.form).then(resp => {
              if (resp) {
                this.cancel()
                this.$parent.loadTableData()
              } else {
                this.msgError(resp.message)
                this.btnDisabled = false
              }
            })
          } else { // 添加
            addCompanyTempType(this.form).then(resp => {
              if (resp) {
                this.cancel()
                this.$parent.loadTableData()
              } else {
                this.msgError(resp.message)
                this.btnDisabled = false
              }
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

